//
//  ZQLoginPage.m
//  jianzhik
//
//  Created by huami on 2018/8/27.
//  Copyright © 2018年 WKWebView. All rights reserved.
//
#import "UIHelper.h"

#import <MJExtension/MJExtension.h>
#import "ZQLoginModels.h"
#import "ZQSaveModels.h"

#import "ZQMainPage.h"
#import "ZQLoginPage.h"
@interface ZQLoginPage ()
@property (weak, nonatomic) IBOutlet UITextField *userTextField;
@property (weak, nonatomic) IBOutlet UITextField *psdTextField;
/** saveModel */
@property (nonatomic, strong) ZQSaveModels *saveModel;

@end

@implementation ZQLoginPage

- (void)viewDidLoad {
    [super viewDidLoad];
    //读取存入的数组 打印
    NSString *Username = [[NSUserDefaults standardUserDefaults] objectForKey:@"Username"];
    RLMResults *results = [ZQSaveModels objectsWhere:@"Username = %@",Username];
    ZQSaveModels *model = results.firstObject;
    if (model.Token.length > 0) {
        ZQMainPage *vc = [ZQMainPage new];
        vc.title = @"管理中心";
        vc.saveModel = model;
        [ZQDataManger shareIntance].loginModels = model;
        [self.navigationController pushViewController:vc animated:NO];
    }
}

- (IBAction)LoginButtonClick:(id)sender
{
    if (self.psdTextField.text.length == 0) {
        [UIHelper showErrorWithText:@"密码不能为空" inView:self.view];
        return;
    }
    
    [UIHelper showMessage:@"正在登陆..." toView:self.view];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"Username"] = self.userTextField.text;
        parameters[@"Password"] = self.psdTextField.text;
    [manager POST:kLoginURL parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        ZQLoginModels *model =  [ZQLoginModels mj_objectWithKeyValues:responseObject];
        ZQSaveModels *saveModel = [ZQSaveModels mj_objectWithKeyValues:responseObject];
        if ([model.Msg isEqualToString:@"登录成功"]) {
            NSLog(@"123");
            if ([saveModel.Msg isEqualToString:@"登录成功"]) {
                saveModel.Username = self.userTextField.text;
                saveModel.Password = self.psdTextField.text;
                saveModel.Token = model.Data.Token;
                saveModel.Id = model.Data.Id;
                saveModel.timeStemp = [[NSDate date] timeIntervalSince1970];
                
                NSString *saveStr  =  [self queryDataWithValue:saveModel.Username].Username;
                if (![saveStr isEqualToString:saveModel.Username]) {
                    [self saveData:saveModel];
                } else {
                    [self changeDataQueryDataWithValue:nil model:saveModel];
                }
                
                [ZQDataManger shareIntance].loginModels = saveModel;
                ZQMainPage *vc = [ZQMainPage new];
                vc.title = @"管理中心";
                vc.saveModel = saveModel;
                [UIHelper dismissLoadingInView:self.view];
                [self.navigationController pushViewController:vc animated:YES];
         }
        } else {
            [UIHelper showErrorWithText:model.Msg inView:self.view];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [UIHelper showErrorWithText:@"服务器请求失败" inView:self.view];
    }];
}


@end
